package com.Jiang.controller;

import com.Jiang.pojo.Book;
import com.Jiang.pojo.Order;
import com.Jiang.pojo.OrderItem;
import com.Jiang.pojo.ShopCar;
import com.Jiang.service.BookService;
import com.Jiang.service.OrderItemService;
import com.Jiang.service.OrderService;
import com.Jiang.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class OrderController {
    @Autowired
    private ShopCarService shopCarService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private BookService bookService;

    // 1.确认订单页面展示
    @RequestMapping("order")
    public String order(HttpSession httpSession, Model model) {
        double sumfee = 0;
        List<ShopCar> checkedCars = shopCarService.checkedShopCar((String) httpSession.getAttribute("Login"));
        for (ShopCar shopCar : checkedCars) {
            sumfee += shopCar.sumFee();
        }
        System.out.println(sumfee);
        System.out.println(checkedCars);
        model.addAttribute("checkedCars", checkedCars);  //给前端传入参数
        model.addAttribute("totalfee", sumfee);
        model.addAttribute("userName", httpSession.getAttribute("userName"));
        return "order";
    }

    //    2.下单
    @RequestMapping("/dealOrder")
    public String test(String telNumber, String address, HttpSession httpSession, Model model, String message, double totalfee, String userName) {
        List<ShopCar> checkedCars = shopCarService.checkedShopCar((String) httpSession.getAttribute("Login"));
        HashMap map = new HashMap();
        HashMap bookMap = new HashMap();
        Random rand = new Random();
        int orderId = rand.nextInt(90000) + 10000 + rand.nextInt(900) + 100;
        Date orderTime = new Date();
//        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
//        System.out.println(orderTime);
//        System.out.println(orderId);
//        System.out.println(telNumber);
//        System.out.println(address);
//        System.out.println(model.getAttribute("checkedCars"));
//        System.out.println(model.getAttribute("totalfee"));
//        System.out.println(totalfee);
        map.put("orderId", orderId);
        map.put("userId", httpSession.getAttribute("Login"));
        map.put("userName", userName);
        map.put("telNumber", telNumber);
        map.put("address", address);
        map.put("payment", totalfee);
        map.put("orderTime", orderTime);
        map.put("message", message);
        System.out.println(map);
        orderService.addOrder(map);

//        *********** 尚未完成 订单详情表  删除购买成功购物车记录
        //添加到订单商品表
        //下单后删除库存量
        for (ShopCar cars : checkedCars) {
            HashMap map1 = new HashMap();
            map1.put("orderId",orderId);
            map1.put("userId",httpSession.getAttribute("Login"));
            map1.put("bookName",cars.getBookName());
            map1.put("num",cars.getNum());
            map1.put("price",cars.getPrice());
            map1.put("url",cars.getUrl());
            bookMap.put("bookCounts",cars.getNum());
            bookMap.put("bookId",cars.getBookId());
            bookService.reduceBookNum(bookMap);
            orderItemService.addItem(map1);
        }
        //删除购物车表
        shopCarService.deleteShopCarByOrder((String) httpSession.getAttribute("Login"));
        return "first";

    }
//      3.用户查询自己的总订单
    @RequestMapping("/userAllOrder")
    public String userAllOrder(HttpSession httpSession,Model model){
        List<Order> orderList = orderService.selectOrder((String)httpSession.getAttribute("Login"));
        model.addAttribute("orderListByuserId",orderList);
        return  "userAllOrder";
    }
//    4.商家查看全部用户的订单
    @RequestMapping("/adminAllOrder")
    public  String adminAllOrder(Model model){
        List<Order> orderList = orderService.selectAllOrder();
        model.addAttribute("allOrderList",orderList);
        return  "adminAllOrder";
    }
//    5.商家根据不同参数查询订单
    @RequestMapping("/adminQueryOrder")
    public String adminQueryOrder(String adminQueryOrder,String Value,Model model){
        System.out.println(adminQueryOrder+"--->"+Value);
        Map map=new HashMap();
        map.put(adminQueryOrder,Value);
        List<Order> adminQueryOrderList = orderService.queryOrder(map);
        System.out.println(adminQueryOrderList);
        model.addAttribute("allOrderList",adminQueryOrderList); //注意此次modle加入的查询出的List应与展示全部订单model所加入的名称一致
        return "adminAllOrder";
    }
//    6.显示详情
    @RequestMapping("/orderDetails")
    public String orderDetails(int orderId,Model model){
        List<OrderItem> lists = orderItemService.selectItemsByOrderId(orderId);
        System.out.println("**********"+lists);
        model.addAttribute("Detais",lists);
        return "orderDetails";
    }
    //    6.用户根据不同参数查询自己的订单
    @RequestMapping("/userQueryOrder")
    public String userQueryOrder(String userQueryOrder,String Value,Model model,HttpSession httpSession){
        System.out.println(userQueryOrder+"--->"+Value);
        Map map=new HashMap();
        map.put(userQueryOrder,Value);
        map.put("userId",httpSession.getAttribute("Login"));
        List<Order> orderList = orderService.userQueryOrder(map);
        System.out.println(orderList);
        model.addAttribute("orderListByuserId",orderList); //注意此次modle加入的查询出的List应与展示全部订单model所加入的名称一致
        return "userAllOrder";
    }

}
